﻿using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Caching;

namespace QD.Cache
{
    public class Caching
    {
        /// <summary>
        /// 缓存前缀
        /// </summary>
        protected static string AppPrefix { get; set; }

        #region  获取缓存内容 public static object Get(string name)
        /// <summary>
        /// 获取缓存内容
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static object Get(string name)
        {
            if (null != HttpContext.Current)
            {
                return HttpContext.Current.Cache.Get(AppPrefix + name);
            }
            else
            {
                return HttpRuntime.Cache.Get(AppPrefix + name);
            }
        }
        #endregion
        public static void Set(string name, object value)
        {
            Set(name, value, (CacheDependency)null);
        }
        public static void Set(string name, object value, string cacheDependency)
        {
            Set(name, value, new CacheDependency(cacheDependency));
        }
        #region 设置缓存 	public static void Set(string name, object value, CacheDependency cacheDependency)
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="cacheDependency"></param>
        public static void Set(string name, object value, CacheDependency cacheDependency)
        {
#if DEBUG
			Set(name, value, cacheDependency, DateTime.Now.AddSeconds(20), TimeSpan.Zero);
#else
            Set(name, value, cacheDependency, DateTime.Now.AddDays(1), TimeSpan.Zero);
#endif
        }
        #endregion
        #region 设置缓存 public static void Set(string name, object value, CacheDependency cacheDependency, DateTime dt)

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="cacheDependency"></param>
        /// <param name="dt"></param>
        public static void Set(string name, object value, CacheDependency cacheDependency, DateTime dt)
        {
            Set(name, value, cacheDependency, dt, TimeSpan.Zero);
        }
        #endregion
        #region 设置缓存 public static void Set(string name, object value, CacheDependency cacheDependency, TimeSpan ts)
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="cacheDependency"></param>
        /// <param name="ts"></param>
        public static void Set(string name, object value, CacheDependency cacheDependency, TimeSpan ts)
        {
            Set(name, value, cacheDependency, System.Web.Caching.Cache.NoAbsoluteExpiration, ts);
        }
        #endregion
        #region 设置缓存 public static void Set(string name, object value, CacheDependency cacheDependency, DateTime dt, TimeSpan ts)
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="cacheDependency"></param>
        /// <param name="dt"></param>
        /// <param name="ts"></param>
        public static void Set(string name, object value, CacheDependency cacheDependency, DateTime dt, TimeSpan ts)
        {
            if (null == value)
            {
                Remove(name);
                return;
            }
            HttpRuntime.Cache.Insert(AppPrefix + name, value, cacheDependency, dt, ts);
        }
        #endregion
        #region 清除缓存 public static void Remove(string name)
        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="name"></param>
        public static void Remove(string name)
        {
            object obj = HttpContext.Current.Cache[AppPrefix + name];
            if (null != obj)
            {
                HttpContext.Current.Cache.Remove(AppPrefix + name);
            }
        }
        #endregion
        #region 清除所有缓存 public static void Remove()
        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public static void Remove()
        {
            IDictionaryEnumerator CacheEnum = HttpContext.Current.Cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (CacheEnum.MoveNext())
            {
                al.Add(CacheEnum.Key);
            }
            foreach (string key in al)
            {
                try
                {
                    HttpContext.Current.Cache.Remove(key);
                }
                catch { }
            }
        }
        #endregion
        #region 获取缓存列表
        public static Dictionary<string, object> GetItemList()
        {
            Dictionary<string, object> list = new Dictionary<string, object>();
            IDictionaryEnumerator CacheEnum = HttpContext.Current.Cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                list.Add(CacheEnum.Key.ToString(), CacheEnum.Value);
            }
            return list;
        }
        #endregion
    }
}
